//TODO 转成链表处理
var head = [1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0]; //输出：18880
// var head = [1, 0, 1];
// var head = [1];

function Node(val) {
    this.val = val;
    this.next = null;
}

class List {
    constructor() {
        this.list = [];
    }
    preNode(index) {
        return this.list[index - 1];
    }

    nextNode(index) {
        return this.list[index + 1];
    }
    insert(val, index = this.list.length) {
        var node = new Node(val);
        this.list[index] = node;

        var preNode = this.preNode(index);
        var nextNode = this.nextNode(index);
        if (nextNode) {
            node.next = nextNode;
        }

        if (preNode) {
            preNode.next = node;
        }
    }
}

var list = new List();

for (let i = 0; i < head.length; i++) {
    list.insert(head[i]);
}

function count(node) {
    if (!node) {
        return 0;
    }
    var count = 0;
    while (node) {
        count = node.val + count * 2;
        node = node.next;
    }
    return count;
}

console.log(count(list.list[0]));